package com.example.lobby.grpc;

import com.example.common.constants.ServiceNames;
import com.example.common.grpc.GrpcClientBase;
import com.example.common.grpc.GrpcClientManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
 * Game服务的gRPC客户端
 * 通过Nacos服务发现连接到game服务
 */
@Slf4j
@Service
public class SeverGrpcClient extends GrpcClientBase {


    public SeverGrpcClient(GrpcClientManager clientManager) {
        super(clientManager);
    }


    @Scheduled(fixedRate = 10000)
    private void heartbeat() {
        try {
            int currentServerId = clientManager.getCurrentServerId();
            log.debug("开始心跳检测，当前服务器ID: {}", currentServerId);

            // 向Lobby服务发送心跳
            sendHeartbeatToService(ServiceNames.GATE);

            // 向Game服务发送心跳
            sendHeartbeatToService(ServiceNames.GAME);

        } catch (Exception e) {
            log.error("心跳检测失败", e);
        }
    }
}